<?php

namespace app\hooks;

use app\api\model\ModeOrders;
use app\api\model\ModeOrdersLog;
use app\api\model\User;
use app\api\model\UserBalanceLog;
use app\api\model\UserIntegralLog;
use think\facade\Db;
use think\helper\Str;

class ModeHook
{
    public static function getUserParents($parent_user_id, $parents = [])
    {
        if ((int)$parent_user_id > 0) {

            if ($parent_user_info = \app\api\model\User::where('id', (int)$parent_user_id)->field(implode(', ', ['id', 'pid', 'mode_one_level_id', 'invest_count', 'mode_three_level_id', 'padian_rate']))->findOrEmpty()->toArray()) {

                $parents[] = $parent_user_info;

                return self::getUserParents($parent_user_info['pid'], $parents);
            }
        }

        return $parents;
    }

    public static function computePercentAmount($amount, $percent, $ratio = 100)
    {
        if ($percent <= 0) {
            return 0;
        }

        if ($percent === $ratio) {
            return $amount;
        }

        return (int)(floor((int)$amount * $percent) / $ratio);
    }

    public static function orderRelease($order_id, $amount, $from_order_id, $type, $speedup_type)
    {

        $release_order_info = ModeOrders::findOrEmpty($order_id)->toArray();
        $from_order_info = ModeOrders::findOrEmpty($from_order_id)->toArray();

        ModeOrders::where('id', $order_id)->dec('remain_earnings', $amount)->update();

        $contents = [];

        if (trim($type)) {
            $contents[] = data_get(ModeOrdersLog::TYPES, $type, '');
        }

        if (trim($speedup_type)) {
            $contents[] = data_get(ModeOrdersLog::SPEEDUP_TYPES, $speedup_type, '');
        }


        ModeOrdersLog::create([
            'user_id' => $release_order_info['user_id'],
            'orders_id' => $release_order_info['id'],
            'amount' => $amount,
            'type' => $type,
            'speedup_benefit_id' => $from_order_id,
            'speedup_type' => $speedup_type,
            'speedup_user' => $from_order_info['user_id'],
            'content' => ($contents = ('['.$from_order_id.']'.implode('-', $contents))),
            'before' => $release_order_info['remain_earnings'],
            'create_time' => date('Y-m-d H:i:s'),
            'is_delete' => 0,
            'status' => 1,
        ]);

        return $contents;
    }

    public static function createIncome($user_id, $balance, $integral, $destroy_amount, $destroy_alias = 'green_points', $type = 'speedup', $log_content = '新增收入')
    {

        if ($balance > 0) {

            User::where('id', $user_id)->inc('balance', $balance)->update();

            UserBalanceLog::create([
                'user_id' => $user_id,
                'amount' => $balance,
                'type' => $type,
                'content' => $log_content,
                'before' => User::where('id', $user_id)->value('balance'),
                'create_time' => date('Y-m-d H:i:s')
            ]);

        }

        if ($integral > 0) {

            User::where('id', $user_id)->inc('integral', $integral)->update();

            UserIntegralLog::create([
                'user_id' => $user_id,
                'amount' => $integral,
                'type' => $type,
                'content' => $log_content,
                'before' => User::where('id', $user_id)->value('integral'),
                'create_time' => date('Y-m-d H:i:s')
            ]);
        }

        if ($destroy_amount > 0) {

            if ($destroy_alias == 'welfare_points') {

                $after = User::where('id', $user_id)->value($destroy_alias);

                User::where('id', $user_id)->dec($destroy_alias, $destroy_amount)->update();

                $before = $after - $destroy_amount;

            } else {

                $before = ModeOrders::where('user_id', $user_id)->where('is_delete', 0)->whereIn('type', ($destroy_alias == 'green_points' ? [1, 2] : [4]))->where('status', 'payed')->whereNotNull('pay_time')->where('remain_earnings', '>', 0)->sum('remain_earnings');

                User::where('id', $user_id)->update([
                    $destroy_alias => $before,
                    'update_time' => date('Y-m-d H:i:s')
                ]);
            }


            $class_alias = Str::studly($destroy_alias);

            $class = "\\app\\api\\model\\User{$class_alias}Log";

            $class::create([
                'user_id' => $user_id,
                'amount' => 0 - $destroy_amount,
                'type' => $type,
                'content' => $log_content,
                'before' => $before,
                'create_time' => date('Y-m-d H:i:s')
            ]);


        }

        return true;
    }


}